// pages/member/bind/index.js
var app = getApp();
var diyad = app.requirejs('biz/ad');
var core = app.requirejs('core');
var $ = app.requirejs('jquery');
var mainui = app.requirejs('mainui');
var api = require('../../../utils/api.js')
Page({

  /**
   * 页面的初始数据
   */
  data: {
    isCheck: false,
    submit: false,
    subtext: "确认绑定",
    endtime: 0,
    postData: {},
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {
    let mobile = app.getCache("mobile")
    this.data.postData.mobile = mobile
    this.setData({
      postData: this.data.postData,
    })
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {
    wx.stopPullDownRefresh()
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  },
  //获取验证码
  getCode: function(event) {
    var $this = this;
    if ($this.data.endtime > 0) {
      return;
    }
    var mobile = $this.data.postData.mobile;
    if (!$.isMobile(mobile)) {
      mainui.toast($this, "请填写正确的手机号");
      return;
    }

    if ($this.data.smsimgcode == 1) {
      var verifyImg = $this.data.postData.verifyImg;
      if (verifyImg == undefined) {
        mainui.toast($this, "请填写图形验证码");
        return;
      }

    }
    let apicontent = api.members.codes()
    core.post(apicontent, {
      phone: $this.data.postData.mobile,
    }, function(result) {
      if (result.code != 1) {
        mainui.toast($this, result.message);
        return;
      }
      mainui.toast($this, "短信发送成功");
      $this.setData({
        endtime: 60
      });
      $this.endTime();
      return;
    }, true, true, true);
  },
  // 提交确认绑定
  submit: function(event) {
    if (this.data.submit) {
      return;
    }
    var $this = this;
    var postData = this.data.postData;
    if (!$.isMobile(postData.mobile)) {
      mainui.toast(this, "请填写正确的手机号");
      return;
    }
    if (!postData.code || postData.code.length != 6) {
      mainui.toast(this, "请填写6位短信验证码");
      return;
    }

    if (!$this.data.isCheck) {
      mainui.toast(this, "请同意隐私条款");
      return;
    }
    this.setData({
      submit: true,
      subtext: "正在绑定..."
    });
    let memberId = app.getCache('userinfo_id');
    let data = postData
    let _this = this
    let apicontent = api.members.bindPhone()
    core.post(apicontent, {
      phone: data.mobile,
      code: data.code
    }, function(ret) {
      if (ret.code != 1) {
        if (!ret.message) {
          ret.message = "验证码错误"
        }
        mainui.toast($this, ret.message);
        $this.setData({
          submit: false,
          subtext: "确认绑定"
        });
        return;
      } else {
        app.setCache("mobile", postData.mobile)
        mainui.toast($this, ret.message)
        $this.setData({
          submit: false,
          subtext: "确认绑定"
        })
        wx.showToast({
          title: '绑定成功',
        })
        wx.navigateBack()
      }
    });
  },
  // 输入信息
  inputChange(event) {
    var postData = this.data.postData;
    var type = core.pdata(event).type;
    var value = event.detail.value;
    postData[type] = $.trim(value);
    this.setData({
      postData: postData
    });
  },
  // 跳转使用条款
  linkRule: function() {
    // wx.setStorageSync('telInput', this.data.telInput)
    wx.navigateTo({
      url: '/pages/custom/index?pageid=102183',
    })
  },
  // 跳转隐私政策
  linkPrivate: function() {
    // wx.setStorageSync('telInput', this.data.telInput)
    wx.navigateTo({
      url: '/pages/custom/index?pageid=102048',
    })
  },
  // 广告关闭 
  closeAdver() {
    this.setData({
      showAdver: false
    })
  },
  //同意条款
  checkboxChange: function(e) {
    var temp = e.detail.value != '' ? true : false
    this.setData({
      isCheck: temp
    })
  },
  //倒计时
  endTime: function() {
    var $this = this;
    var endtime = $this.data.endtime;
    if (endtime > 0) {
      $this.setData({
        endtime: endtime - 1
      });
      var end = setTimeout(function() {
        $this.endTime();
      }, 1000);
    }
    return;
  },
})